package com.hxzy.authentication.service.impl;

import cn.hxzy.common.GenderStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxzy.authentication.domain.TsRealNameAuthentication;
import com.hxzy.authentication.mapper.TsRealNameAuthenticationMapper;
import com.hxzy.authentication.service.TsRealNameAuthenticationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mingqian.Wu
 * @since 2023-06-23
 */
@Service
@DataSource(DataSourceType.AUTHENTICATION)
public class TsRealNameAuthenticationServiceImpl extends ServiceImpl<TsRealNameAuthenticationMapper, TsRealNameAuthentication> implements TsRealNameAuthenticationService {


    @Override
    public List<TsRealNameAuthentication> searchCertification(TsRealNameAuthentication tsRealNameAuthentication) {
        QueryWrapper<TsRealNameAuthentication>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(tsRealNameAuthentication.getRegisterPhone()),"register_phone",tsRealNameAuthentication.getRegisterPhone());
        queryWrapper.eq(Objects.nonNull(tsRealNameAuthentication.getUserId()),"user_id",tsRealNameAuthentication.getUserId());
        queryWrapper.eq(Objects.nonNull(tsRealNameAuthentication.getIdNumber()),"id_number",tsRealNameAuthentication.getIdNumber());
        queryWrapper.like(Objects.nonNull(tsRealNameAuthentication.getName()),"name",tsRealNameAuthentication.getName());
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        queryWrapper.notInSql("certification_status","101");
        if (Objects.nonNull(tsRealNameAuthentication.getValidPeriod())){
            List<TsRealNameAuthentication> list = baseMapper.selectAuthenticationByValidPeriod(tsRealNameAuthentication.getValidPeriod());
            return list;
        }

        if (Objects.nonNull(tsRealNameAuthentication.getApplyTime())){
            String applyTime = simpleDateFormat.format(tsRealNameAuthentication.getApplyTime());
            queryWrapper.like("apply_time",applyTime);
        }

        List<TsRealNameAuthentication> tsRealNameAuthentications = baseMapper.selectList(queryWrapper);
        return tsRealNameAuthentications;
    }



    public Map<String, String> getIdNumber(List<String> ids) {

        //根据用户id查询身份证号码
        List<TsRealNameAuthentication> list = baseMapper.selectList(
                new LambdaQueryWrapper<TsRealNameAuthentication>()
                        .select(TsRealNameAuthentication::getUserId, TsRealNameAuthentication::getIdNumber)
                        .in(Objects.nonNull(ids) && ids.size() > 0, TsRealNameAuthentication::getUserId, ids)
        );

        //将userId:idNumber存放到map中
        Map<String, String> idNumber = list.stream().collect(Collectors.toMap(item -> item.getUserId(), item -> item.getIdNumber()));


        return idNumber;
    }

    /**
     * 根据用户id查询实名认证信息
     * @param userId
     * @return
     */

    public TsRealNameAuthentication getRealNameInfo(String userId) {

        TsRealNameAuthentication tsRealNameAuthentication = baseMapper.selectOne(new LambdaQueryWrapper<TsRealNameAuthentication>().eq(StringUtils.hasLength(userId), TsRealNameAuthentication::getUserId, userId));
        tsRealNameAuthentication.setGender(GenderStatus.getGenderStatusValue(Integer.parseInt(tsRealNameAuthentication.getGender())));
        return tsRealNameAuthentication;
    }


}
